package com.xhcl.web.controller.system;


import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.codec.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.xhcl.common.annotation.Log;
import com.xhcl.common.core.controller.BaseController;
import com.xhcl.common.core.domain.AjaxResult;
import com.xhcl.common.core.page.TableDataInfo;
import com.xhcl.common.enums.BusinessType;
import com.xhcl.common.exception.BusinessException;
import com.xhcl.common.utils.DateUtils;
import com.xhcl.framework.uitls.poi.ExcelUtil;
import com.xhcl.framework.shiro.util.ShiroUtils;
import com.xhcl.system.domain.TGgKmdm;
import com.xhcl.system.domain.TSrhsJtmx;
import com.xhcl.system.domain.TSrhsPzti;
import com.xhcl.system.domain.TSrhsPztiFzhs;
import com.xhcl.system.domain.TSrhsPzto;
import com.xhcl.system.service.ITGgKmdmService;
import com.xhcl.system.service.ITSrhsHsxtcsService;
import com.xhcl.system.service.ITSrhsJtmxService;
import com.xhcl.system.service.ITSrhsPztiFzhsService;
import com.xhcl.system.service.ITSrhsPztiService;
import com.xhcl.system.service.ITSrhsPztoService;

/**
 * 凭证头Controller
 * 
 * @author xhcl
 * @date 2020-09-06
 */
@Controller
@RequestMapping("/system/pzto")
public class TSrhsPztoController extends BaseController
{
    private String prefix = "system/pzto";

    @Autowired
    private ITSrhsPztoService tSrhsPztoService;
    
    @Autowired
    private ITSrhsHsxtcsService tSrhsHsxtcsService;
    @Autowired
    private ITSrhsPztiService tSrhsPztiService;
    

    @Autowired
    private ITSrhsJtmxService tSrhsJtmxService;
    
    @Autowired
    private ITSrhsPztiFzhsService tSrhsPztiFzhsService;
    @Autowired
    private ITGgKmdmService tGgKmdmService;

    @RequiresPermissions("system:pzto:view")
    @GetMapping()
    public String pzto(ModelMap mmap)
    {  
    	String year=tSrhsHsxtcsService.getHsxtcsnr("year");
    	String term=tSrhsHsxtcsService.getHsxtcsnr("term");
    	List<String> ndlist=new ArrayList();
    	String curDate=DateUtils.getDate();
    	if (!curDate.startsWith(year)) {
    		ndlist.add(String.valueOf(Integer.parseInt(year)+1));
    	}
    	for (int i=0;i<5;i++) {
    		ndlist.add(String.valueOf(Integer.parseInt(year)-i));
    	}
    	mmap.put("ndlist", ndlist);
    	mmap.put("year", year);
    	List<String> yflist=new ArrayList();
    	for (int i=1;i<=12;i++) {
    		yflist.add(String.valueOf(i));
    	}
    	mmap.put("yflist", yflist);
    	mmap.put("term", term);
        return prefix + "/pzto";
    }

    /**
     * 查询凭证头列表
     */
    @RequiresPermissions("system:pzto:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TSrhsPzto tSrhsPzto)
    {
        startPage();
        List<TSrhsPzto> list = tSrhsPztoService.selectTSrhsPztoList(tSrhsPzto);
        return getDataTable(list);
    }

    /**
     * 导出凭证头列表
     */
    @RequiresPermissions("system:pzto:export")
    @Log(title = "凭证头", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TSrhsPzto tSrhsPzto)
    {
        List<TSrhsPzto> list = tSrhsPztoService.selectTSrhsPztoList(tSrhsPzto);
        ExcelUtil<TSrhsPzto> util = new ExcelUtil<TSrhsPzto>(TSrhsPzto.class);
        return util.exportExcel(list, "pzto");
    }

    /**
     * 新增凭证头
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }
    
    @GetMapping(value={"/addmx/{index}/{kmdm}/{fzhsdw}"})
    public String addmx(@PathVariable("index") Long index, @PathVariable("kmdm") String kmdm, @PathVariable("fzhsdw") String fzhsdw,String fzmx,ModelMap mmap) throws UnsupportedEncodingException
    {   
    	
    	mmap.put("index", index);
    	mmap.put("kmdm", kmdm);
    	mmap.put("fzhsdw", fzhsdw);
    	if (StringUtils.isNotEmpty(fzmx)){
    		String base64Str= new String(Base64.decode(fzmx),"GBK");
    		List<Map> list=JSON.parseArray(base64Str, Map.class);
    	  mmap.put("fzmx", list);
    	}else{
    		List list=new ArrayList();
    		mmap.put("fzmx", list);
    	}
        return prefix + "/addmx";
    }

    /**
     * 新增保存凭证头
     * @throws UnsupportedEncodingException 
     * @throws BusinessException 
     */
    @RequiresPermissions("system:pzto:add")
    @Log(title = "凭证头", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TSrhsPzto tSrhsPzto) throws BusinessException, UnsupportedEncodingException
    {  
    	String operName = ShiroUtils.getSysUser().getLoginName();
    	tSrhsPzto.setZd(operName);
        return toAjax(tSrhsPztoService.insertTSrhsPzto(tSrhsPzto));
    }

    /**
     * 修改凭证头
     */
    @GetMapping("/edit/{pztxh}")
    public String edit(@PathVariable("pztxh") Long pztxh, ModelMap mmap)
    {
        TSrhsPzto tSrhsPzto = tSrhsPztoService.selectTSrhsPztoById(pztxh);
        TSrhsPzti tSrhsPzti=new TSrhsPzti(); 
        tSrhsPzti.setPztxh(pztxh);
        List<TSrhsPzti> listPzti= tSrhsPztiService.selectTSrhsPztiList(tSrhsPzti);
        listPzti.forEach(m->{
        	TGgKmdm tGgKmdm=new TGgKmdm();
        	tGgKmdm.setKmdm(m.getKmdm());
        	tGgKmdm.setNd(tSrhsPzto.getNd());
        	List<TGgKmdm> kmlist=tGgKmdmService.selectTGgKmdmList(tGgKmdm);
        	if (kmlist.stream().findFirst().orElse(new TGgKmdm()).getFzhsdw()>0) {
	        	TSrhsPztiFzhs tSrhsPztiFzhs=new TSrhsPztiFzhs();
	        	tSrhsPztiFzhs.setPztxh(m.getPztxh());
	        	tSrhsPztiFzhs.setXh(m.getXh());
	        	List<TSrhsPztiFzhs> list=tSrhsPztiFzhsService.selectTSrhsPztiFzhsList(tSrhsPztiFzhs);
	        	m.setFzmx(JSON.toJSONString(list));
        	}
        });
        mmap.put("tSrhsPzto", tSrhsPzto);
        mmap.put("listPzti", listPzti);
        return prefix + "/edit";
    }

    /**
     * 修改保存凭证头
     */
    @RequiresPermissions("system:pzto:edit")
    @Log(title = "凭证头", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TSrhsPzto tSrhsPzto)
    {
        return toAjax(tSrhsPztoService.updateTSrhsPzto(tSrhsPzto));
    }

    /**
     * 删除凭证头
     */
    @RequiresPermissions("system:pzto:remove")
    @Log(title = "凭证头", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tSrhsPztoService.deleteTSrhsPztoByIds(ids));
    }
    
    /**
     * 删除
     */
    @Log(title = "删除凭证", businessType = BusinessType.DELETE)
    @RequiresPermissions("system:pzto:remove")
    @PostMapping("/remove/{pztxh}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("pztxh") Long pztxh)
    {
    	tSrhsPztoService.deleteTSrhsPztoById(pztxh); 
        return AjaxResult.success();
    }
    @Log(title = "凭证记账", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:pzto:account")
    @PostMapping("/account/{pztxh}")
    @ResponseBody
    public AjaxResult account(@PathVariable("pztxh") Long pztxh)
    {
    	String operName = ShiroUtils.getSysUser().getLoginName();
    	tSrhsPztoService.accountVoucherByPztxh(pztxh, operName);
        return AjaxResult.success();
    }
    
    @Log(title = "凭证反记账", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:pzto:unaccount")
    @PostMapping("/unaccount/{pztxh}")
    @ResponseBody
    public AjaxResult unaccount(@PathVariable("pztxh") Long pztxh)
    {
    	String operName = ShiroUtils.getSysUser().getLoginName();
    	tSrhsPztoService.unaccountVoucherByPztxh(pztxh, operName);
        return AjaxResult.success();
    }
    /**
     * 批量记账
     */
    @RequiresPermissions("system:pzto:bataccount")
    @Log(title = "批量记账", businessType = BusinessType.DELETE)
    @PostMapping( "/bataccount")
    @ResponseBody
    public AjaxResult bataccount(String ids)
    {   
    	String operName = ShiroUtils.getSysUser().getLoginName();
        return toAjax(tSrhsPztoService.accountVoucherByPztxhs(ids, operName));
    }
    
    
    /**
     * 批量记账
     */
    @RequiresPermissions("system:pzto:batunaccount")
    @Log(title = "批量反记账", businessType = BusinessType.DELETE)
    @PostMapping( "/batunaccount")
    @ResponseBody
    public AjaxResult batunaccount(String ids)
    {   
    	String operName = ShiroUtils.getSysUser().getLoginName();
        return toAjax(tSrhsPztoService.unaccountVoucherByPztxhs(ids, operName));
    }
    
    
    @GetMapping("/pzlist/{pztxh}")
    public String pzlist(@PathVariable("pztxh") Long pztxh, ModelMap mmap)
    {
        TSrhsPzto tSrhsPzto = tSrhsPztoService.selectTSrhsPztoById(pztxh);
        mmap.put("tSrhsPzto", tSrhsPzto);
        return prefix + "/pzlist";
    }
    
    /**
     * 查询凭证头列表
     */
    @RequiresPermissions("system:pzto:list")
    @PostMapping("/pztilist")
    @ResponseBody
    public TableDataInfo list(TSrhsPzti tSrhsPzti)
    {   
        List<TSrhsPzti> list = tSrhsPztiService.selectTSrhsPztiList(tSrhsPzti);
        return getDataTable(list);
    }
    @RequiresPermissions("system:pzto:list")
    @PostMapping("/jtmxlist")
    @ResponseBody
    public TableDataInfo jtmxlist(TSrhsJtmx tSrhsJtmx)
    {   
    	List<TSrhsJtmx> list = tSrhsJtmxService.selectTSrhsJtmxList(tSrhsJtmx);
        return getDataTable(list);
    }
    
    
    /**
     * 断号检查头
     * @throws UnsupportedEncodingException 
     * @throws BusinessException 
     */
    @RequiresPermissions("system:pzto:checkbreak")
    @Log(title = "断号检查", businessType = BusinessType.OTHER)
    @PostMapping("/checkbreak")
    @ResponseBody
    public AjaxResult checkbreak(TSrhsPzto tSrhsPzto) throws BusinessException, UnsupportedEncodingException
    {  
    	String chk=tSrhsPztoService.doBreakCheck(tSrhsPzto.getNd(), tSrhsPzto.getYf());
        return AjaxResult.success("0".equals(chk)?"没有断号":chk);
    }
    
    /**
     * 断号检查头
     * @throws UnsupportedEncodingException 
     * @throws BusinessException 
     */
    @RequiresPermissions("system:pzto:modifydate")
    @Log(title = "断号检查", businessType = BusinessType.OTHER)
    @PostMapping("/modifydate")
    @ResponseBody
    public AjaxResult modifydate(TSrhsPzto tSrhsPzto) throws BusinessException, UnsupportedEncodingException
    {  
    	String rst=tSrhsPztoService.modifyDate(tSrhsPzto.getPzrq(), tSrhsPzto.getPztxh());
        return AjaxResult.success("ok".equals(rst)?"修改成功":rst);
    }
    
}
